class GameStartScene extends GameScene {
    public constructor() {
        super();
        this.start = ()=> {
            this.createScene();
            this.loadUser();
        }
        this.end = ()=> {
            this.parent.removeChild(this);
        }
    }
    private user: User;
    private textfield: egret.TextField;

    /**
     * 创建游戏初始场景
     * Create a game start scene
     */
    private createScene() {
        let sky = this.createBitmapByName("bg_jpg");
        //this.addChild(sky);
        let stageW = this.width;
        let stageH = this.height;
        sky.width = stageW;
        sky.height = stageH;

        let topMask = new egret.Shape();
        topMask.graphics.beginFill(0xffffff, 0.5);
        topMask.graphics.drawRect(0, 0, stageW, 172);
        topMask.graphics.endFill();
        topMask.y = 33;
        topMask.alpha = 0.5;
        this.addChild(topMask);

        let icon = this.createBitmapByName("egret_icon_png");
        this.addChild(icon);
        icon.x = 26;
        icon.y = 33;

        let line = new egret.Shape();
        line.graphics.lineStyle(2, 0xffffff);
        line.graphics.moveTo(0, 0);
        line.graphics.lineTo(0, 117);
        line.graphics.endFill();
        line.x = 172;
        line.y = 61;
        this.addChild(line);


        let colorLabel = new egret.TextField();
        colorLabel.textColor = 0xffffff;
        colorLabel.width = stageW - 172;
        colorLabel.textAlign = "center";
        colorLabel.text = "Hello Egret";
        colorLabel.size = 24;
        colorLabel.x = 172;
        colorLabel.y = 80;
        this.addChild(colorLabel);

        let textfield = new egret.TextField();
        this.addChild(textfield);
        textfield.alpha = 0;
        textfield.width = stageW - 172;
        textfield.textAlign = egret.HorizontalAlign.CENTER;
        textfield.size = 24;
        textfield.textColor = 0xffffff;
        textfield.x = 172;
        textfield.y = 135;
        this.textfield = textfield;
    }

    /**
     * 根据name关键字创建一个Bitmap对象。name属性请参考resources/resource.json配置文件的内容。
     * Create a Bitmap object according to name keyword.As for the property of name please refer to the configuration file of resources/resource.json.
     */
    private createBitmapByName(name: string) {
        let result = new egret.Bitmap();
        let texture: egret.Texture = RES.getRes(name);
        result.texture = texture;
        return result;
    }

    /**
     * 切换描述文字
     */
    private changeText(text:string) {
        let tw = egret.Tween.get(this.textfield);
        tw.to({ "alpha": 0 }, 200);
        this.textfield.textFlow = new egret.HtmlTextParser().parse(text);
        tw.to({ "alpha": 1 }, 200);
    }
    /**
     * 加载用户数据
     */
    private loadUser():void {
        var request = new egret.HttpRequest();
        request.responseType = egret.HttpResponseType.TEXT;
        request.open("/loadUserInfo",egret.HttpMethod.GET);
        request.addEventListener(egret.ProgressEvent.PROGRESS,this.onLoadUserProgress,this);
        request.addEventListener(egret.Event.COMPLETE,this.onLoadUserComplete,this);
        request.addEventListener(egret.IOErrorEvent.IO_ERROR,this.onLoadUserError,this);
        request.send();
        this.changeText("<font color=0xffffff>玩家信息加载中...</font>");
    }
    private onLoadUserProgress(event:egret.ProgressEvent):void {
        console.log("onLoadUserProgress : " + Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%");
        this.changeText("<font color=0xffffff>玩家信息加载中...（"+Math.floor(100*event.bytesLoaded/event.bytesTotal) + "%"+"）</font>");
    }
    private onLoadUserComplete(event:egret.Event):void {
        // var request = <egret.HttpRequest>event.currentTarget;
        this.user = new User();
        this.user.goldCount = 9999;
        this.changeText("<font color=0x00ff0c>玩家信息加载成功</font>");
        
        //定时跳转到游戏playing场景
        var timer:egret.Timer = new egret.Timer(1000, 1);
        timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,this.goToNextScene,this);
        timer.start();
    }
    private onLoadUserError(event:egret.IOErrorEvent):void {
        this.changeText("<font color=0xff3300>玩家信息加载失败，请联系管理员</font>");
    }
    /**
     * 自动跳转的游戏playing场景
     */
    private goToNextScene() {
        this.changeText("<font color=0x00ff0c>正在前往游戏场景</font>");
        var timer:egret.Timer = new egret.Timer(1000, 1);
            timer.addEventListener(egret.TimerEvent.TIMER_COMPLETE,function(){
            let nextScene = new GamePlayingScene(this.user);
            this.parent.addChild(nextScene);
            this.end();
            nextScene.start();
        },this);
        timer.start();

    }
}
